<?php
/**
 * BrandsController.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-13 22:11:03
 * @modified    2023-02-13 22:11:03
 *
 */

namespace App\Http\Controllers;

use App\Repositories\Product\BrandRepo;
use Illuminate\Http\Request;

class BrandsController extends BaseController
{
    /**
     * @OA\Get(
     *     path="/brands",
     *     tags={"Brand"},
     *     summary="获取品牌分类列表",
     *     description="获取品牌分类列表",
     *     operationId="brand_index",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="name", in="query", description="品牌名称", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="status", in="query", description="状态", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="page", in="query", description="当前页数", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="limit", in="query", description="每页数量(默认10)", required=false, @OA\Schema(default=10, type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401, description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function index(Request $request)
    {
        $brands = BrandRepo::getList();
        // return message("获取成功", true, $brands);
        return list_message("获取成功", true, $brands);
    }

    /**
     * @OA\Get(
     *     path="/brands/{id}",
     *     tags={"Brand"},
     *     summary="获取品牌详情",
     *     description="获取品牌详情",
     *     operationId="brand_show",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id", in="path", description="品牌ID", required=true, @OA\Schema(default="", type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401, description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function show(Request $request, $id)
    {
        try {
            $brand = BrandRepo::fetch($id);
            return message("获取成功", true, $brand);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Post(
     *     path="/brands",
     *     tags={"Brand"},
     *     summary="添加品牌",
     *     description="添加品牌",
     *     operationId="brand_store",
     *     security={{"bearerAuth":{}}},
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"name", "image", "sort_order", "status"},
     *              @OA\Property(property="name", type="string", example="", description="品牌名称"),
     *              @OA\Property(property="image", type="string", example="", description="品牌图片"),
     *              @OA\Property(property="sort_order", type="integer", example=0, description="排序"),
     *              @OA\Property(property="status", type="integer", example=1, description="排序")
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401, description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function store(Request $request)
    {
        try {
            $brand = BrandRepo::store(0);
            return message("创建成功", true, $brand);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Put(
     *     path="/brands/{id}",
     *     tags={"Brand"},
     *     summary="更新品牌",
     *     description="更新品牌",
     *     operationId="brand_update",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(
     *         name="id",
     *         in="path",
     *         description="品牌ID",
     *         required=false,
     *         @OA\Schema(
     *             default="",
     *             type="integer"
     *         )
     *     ),
     *     @OA\Parameter(name="id", in="path", description="品牌ID", required=true, @OA\Schema(default="", type="integer")),
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"name", "image", "sort_order", "status"},
     *              @OA\Property(property="name", type="string", example="", description="品牌名称"),
     *              @OA\Property(property="image", type="string", example="", description="品牌图片"),
     *              @OA\Property(property="sort_order", type="integer", example=0, description="排序"),
     *              @OA\Property(property="status", type="integer", example=1, description="排序")
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401, description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function update(Request $request, $id)
    {
        try {
            $brand = BrandRepo::store($id);
            return message("更新成功", true, $brand);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Delete (
     *     path="/brands/{id}",
     *     tags={"Brand"},
     *     summary="删除品牌",
     *     description="删除品牌",
     *     operationId="brand_delete",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id", in="path", description="品牌ID", required=true, @OA\Schema(default="", type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401, description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function destroy(Request $request, $id)
    {
        try {
            BrandRepo::destroy($id);
            return message("删除成功", true);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }
}